<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/14
 * Time: 10:17
 */

namespace app\admin\controller;


use app\admin\model\ShopIcon;
use app\admin\model\ShopLunbo;
use app\admin\model\ShopMessage;
use app\admin\model\ShopMessageType;
use think\Controller;
use think\Db;

class Shopinformation extends Controller
{
    //=================================================信息分类列表
    public function messageType(){
        return $this->fetch();
    }

    //=================================================信息分类数据
    public function messageTypeData($page,$limit){
        $data=ShopMessageType::page($page,$limit)->order('sort asc')->select();
        $count=ShopMessageType::count();
        $res=['code'=>0,'count'=>$count,'data'=>$data];
        return json($res);
    }

    //================================================添加信息分类
    public function messageTypeAdd(){
        if(request()->isPost()){
            $post=input('post.');
            $data['name']=$post['name'];
            $data['sort']=$post['sort'];
            $addres=ShopMessageType::insert($data);
            if($addres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }
        return $this->fetch();
    }

    //================================================添加信息分类
    public function messageTypeEdit(){
        if(request()->isPost()){
            $post=input('post.');
            $data['name']=$post['name'];
            $data['sort']=$post['sort'];
            $saveres=ShopMessageType::where('id',$post['id'])->update($data);
            if($saveres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }
        //查询需要修改的信息分类
        $messageType=ShopMessageType::where('id',input('get.id'))->find();
        $this->assign('messageType',$messageType);
        return $this->fetch();
    }

    //===============================================删除分类信息
    public function messageTypeDel(){
        $delres=ShopMessageType::where('id',input('post.id'))->delete();
        if($delres){
            $res=['code'=>1,'msg'=>'保存成功'];
        }else{
            $res=['code'=>0,'msg'=>'保存失败'];
        }
        return json($res);
    }

    //===============================================文章管理
    public function message(){
        return $this->fetch();
    }

    //===============================================文章内容
    public function messageData($page,$limit){
        $data=ShopMessage::page($page,$limit)->order('sort asc')->select();
        $count=ShopMessage::count();
        $res=['code'=>0,'count'=>$count,'data'=>$data];
        return json($res);
    }

    //===============================================添加文章信息
    public function messageAdd(){
        if(request()->isPost()){
            $post=input('post.');
            $data['title']=$post['title'];
            $data['image']=$post['image'];
            $data['type']=$post['type'];
            $data['pulishtime']=time();
            $data['sort']=$post['sort'];
            $data['content']=$post['content'];
            $addres=ShopMessage::insert($data);
            if($addres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }
        return $this->fetch();
    }

    //================================================修改文章信息
    public function messageEdit(){
        if(request()->isPost()){
            $post=input('post.');
            $data['title']=$post['title'];
            $data['image']=$post['image'];
            $data['type']=$post['type'];
            $data['sort']=$post['sort'];
            $data['content']=$post['content'];
            $saveres=ShopMessage::where('id',$post['id'])->update($data);
            if($saveres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }

        //查询需要修改的文章信息
        $messageInfo=ShopMessage::where('id',input('get.id'))->find();
        $this->assign('messageInfo',$messageInfo);
        return $this->fetch();
    }

    //==================================================删除文章信息
    public function messageDel(){
        $delres=ShopMessage::where('id',input('post.id'))->delete();
        if($delres){
            $res=['code'=>1,'msg'=>'保存成功'];
        }else{
            $res=['code'=>0,'msg'=>'保存失败'];
        }
        return json($res);
    }

    //==================================================轮播列表
    public function lunbo(){
        return $this->fetch();
    }

    //==================================================轮播数据
    public function lunboData($page,$limit){
        $data=ShopLunbo::page($page,$limit)->order('sort desc')->select();
        $count=ShopLunbo::count();
        $res=['code'=>0,'count'=>$count,'data'=>$data];
        return json($res);
    }

    //==================================================添加轮播
    public function lunboAdd(){
        if(request()->isPost()){
            $post=input('post.');
            $data['image']=$post['image'];
            $data['sort']=$post['sort'];
            $data['url']=$post['url'];
            $addres=ShopLunbo::insert($data);
            if($addres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }
        return $this->fetch();
    }

    //=================================================修改轮播
    public function lunboEdit(){
        if(request()->isPost()){
            $post=input('post.');
            $data['image']=$post['image'];
            $data['sort']=$post['sort'];
            $data['url']=$post['url'];
            $saveres=ShopLunbo::where('id',$post['id'])->update($data);
            if($saveres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }
        //修改轮播信息
        $lunboInfo=ShopLunbo::where('id',input('get.id'))->find();
        $this->assign('lunboInfo',$lunboInfo);
        return $this->fetch();
    }

    //=================================================修改轮播
    public function lunboDel(){
        $delres=ShopLunbo::where('id',input('post.id'))->delete();
        if($delres){
            $res=['code'=>1,'msg'=>'保存成功'];
        }else{
            $res=['code'=>0,'msg'=>'保存失败'];
        }
        return json($res);
    }
    //首页图标列表
    public function iconList(){
        return $this->fetch();
    }
    public function iconListData($page,$limit){
        $data=ShopIcon::page($page,$limit)->order('icon_sort desc')->select();
        $count=ShopIcon::count();
        $res=['code'=>0,'count'=>$count,'data'=>$data];
        return json($res);

    }
    //首页图标添加
    public function iconAdd(){
        return $this->fetch();
    }
    public function iconAddForm(){
        $post=input('post.');
        $data=[
            'icon_sort'=>$post['icon_sort'],
            'icon_name'=>$post['icon_name'],
            'icon_logo'=>$post['icon_logo'],
            'icon_url'=>$post['icon_url'],
        ];
        $save=ShopIcon::insert($data);
        if($save){
            return json(['code'=>1,'msg'=>'保存成功']);
        }else{
            return json(['code'=>0,'msg'=>'保存失败']);
        }
    }
    //首页图标修改
    public function iconEdit(){
        $whereAA['id']=input('get.id');
        $iconRes=ShopIcon::where($whereAA)->find();
        $this->assign('data',$iconRes);
        return $this->fetch();
    }
    public function iconEditForm(){
        $post=input('post.');
        $data=[
            'icon_sort'=>$post['icon_sort'],
            'icon_name'=>$post['icon_name'],
            'icon_logo'=>$post['icon_logo'],
            'icon_url'=>$post['icon_url'],
        ];
        $whereAB['id']=$post['id'];
        $save=ShopIcon::where($whereAB)->update($data);
        if($save){
            return json(['code'=>1,'msg'=>'保存成功']);
        }else{
            return json(['code'=>0,'msg'=>'保存失败']);
        }
    }
    public function iconDel(){
        $post=input('post.id');
        $count=count($post);
        for($i=0;$i<$count;$i++){
            $whereAA['id']=$post[$i];
            $del=ShopIcon::where($whereAA)->delete();
        }
        if($del){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }
    }

    public function shopImage(){
        $file =request()->file('file');
        $info = $file->move( '../public/uploads');
        if ($info) {
            $result = [
                'code'     => 1,
                'msg'      => '上传成功',
                'url' => '/uploads/' . str_replace('\\', '/', $info->getSaveName())
            ];
        } else {
            $result = [
                'code' => 0,
                'msg'  => $file->getError()
            ];
        }
        return $result;
    }
}